package com.leon.alishop.spider.worker;

import java.util.HashMap;
import java.util.List;
import java.util.Vector;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.leon.alishop.core.config.AlishopConfig;
import com.leon.alishop.core.config.ProviderConstant;
import com.leon.alishop.core.util.AliShopStringUtils;
import com.leon.alishop.dao.ProviderInfoDao;
import com.leon.alishop.model.ProviderInfo;
import com.leon.alishop.spider.bucket.MultStringBucket;
import com.leon.alishop.spider.bucket.ProviderBucket;
import com.leon.alishop.spider.enumeration.CrawlFailTypeEnum;
import com.leon.alishop.spider.model.TaobaoGoodsInfo;
import com.leon.alishop.spider.service.TaobaoGoodsService;
import com.leon.alishop.spider.spider.CrawlTaobaoSpider;

public class TaobaoShopCrawlThread implements Runnable{
	
	private static Logger log = LoggerFactory.getLogger(TaobaoShopCrawlThread.class);

	private static volatile boolean isRun;
	
	private Thread currentThread;
	
	private MultStringBucket handledUrls = new MultStringBucket(1000000);
	
	private volatile static ProviderBucket providerBucket;
	
	private volatile static HashMap<CrawlFailTypeEnum, Vector<Integer>> crawlFailProviderMap;
	
	public static boolean isRun(){
		return isRun;
	}
	
	

	@Override
	public void run() {
		if(isRun)
			return;
		isRun = true;
		currentThread = Thread.currentThread();
		ProviderInfo pro;
		List<String> itemUrlList;
		int count;
		long timemill;
		long totalTimemill = System.currentTimeMillis();
		int totalCount = 0;
		while ((pro = providerBucket.nextProviderInfo()) != null ) {
			if(pro.getStatus() == null || pro.getStatus().intValue()==1)
				continue;
			AlishopConfig.getInstance().setLastCrawlProviderId(pro.getId());
			timemill = System.currentTimeMillis();
			log.info("[ProviderId:"+pro.getId()+"]开始爬行《"+pro.getName()+"》");
			itemUrlList = crawlTaobaoSpider.crawlAllItemUrl(pro);
			count = itemUrlList==null ? 0 : itemUrlList.size();
			
			log.info("[ProviderId:"+pro.getId()+"]开始爬行ItemUrl集合："+ count);
			List<TaobaoGoodsInfo> goodsList = crawlTaobaoSpider.crawlItemUrl(pro, itemUrlList);
			count = goodsList==null ? 0 : goodsList.size();
			
			log.info("[ProviderId:"+pro.getId()+"]开始处理TaobaoGoodsInfo集合："+ count);
			taobaoGoodsService.insertTaobaoGoodsSku(pro,goodsList,true);
			
			timemill = System.currentTimeMillis() - timemill;
			log.info("[ProviderId:"+pro.getId()+"]爬行完毕，处理商品"+count+"条，耗时：" + AliShopStringUtils.sec2Time(timemill));
			
			pro.setValidGoodsCount(count);
			providerInfoDao.updateByIdSelective(pro);
			totalCount += count;
		}
		log.info("爬行结束，总处理商品"+totalCount+"条，总耗时：" + AliShopStringUtils.sec2Time(System.currentTimeMillis()-totalTimemill));
		isRun = false;
	}
	
	public synchronized void init(boolean continueFlag){
		if(providerBucket == null){
			providerBucket = new ProviderBucket(ProviderConstant.getInstance().getProviderInfoList());
		}
		if(crawlFailProviderMap == null){
			crawlFailProviderMap = new HashMap<CrawlFailTypeEnum, Vector<Integer>>();
			CrawlFailTypeEnum[] values = CrawlFailTypeEnum.values();
			for (CrawlFailTypeEnum _type : values) {
				crawlFailProviderMap.put(_type, new Vector<Integer>());
			}
		}
		if(continueFlag){
			Integer lastProviderId = AlishopConfig.getInstance().getLastCrawlProviderId();
			if(null != lastProviderId){
				providerBucket.setPosToProviderId(lastProviderId);
			}
		}else{
			providerBucket.reset();
		}
	}
	
	private CrawlTaobaoSpider crawlTaobaoSpider;
	
	private TaobaoGoodsService taobaoGoodsService;
	
	private ProviderInfoDao providerInfoDao;

	public void setCrawlTaobaoSpider(CrawlTaobaoSpider crawlTaobaoSpider) {
		this.crawlTaobaoSpider = crawlTaobaoSpider;
	}

	public void setTaobaoGoodsService(TaobaoGoodsService taobaoGoodsService) {
		this.taobaoGoodsService = taobaoGoodsService;
	}
	
	public void setProviderInfoDao(ProviderInfoDao providerInfoDao) {
		this.providerInfoDao = providerInfoDao;
	}
}
